package com.shtel.paas.monitorcenter.config.management.alarm.config.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.shtel.paas.monitorcenter.config.management.alarm.config.entity.AlarmRuleHostDeleteDto;

import com.shtel.paas.monitorcenter.config.management.alarm.config.entity.AlarmRulesHostEntity;

import com.shtel.paas.monitorcenter.config.management.alarm.config.entity.AlarmRulesHostVO;
import com.shtel.paas.monitorcenter.config.management.alarm.config.entity.AlarmRulesHostidDto;
import com.shtel.paas.monitorcenter.config.management.alarm.config.mapper.WarningPersonalizedConfigDao;
import com.shtel.paas.monitorcenter.config.management.alarm.config.service.WarningPersonalizedConfigService;


/**  
* @Description: 接口实现
* @author zhangyy  
* @date 2018年5月31日  
*/
@Service
public class WarningPersonalizedConfigServiceImpl implements WarningPersonalizedConfigService{
	
	@Autowired
	WarningPersonalizedConfigDao dao;
	@Override
	public PageInfo<AlarmRulesHostVO> selectAll(String monitoringIndex,Integer page, Integer pageSize) {
		//分页页数不对
		List<AlarmRulesHostEntity> list = dao.selectMonitorIndex(monitoringIndex);
		int a=list.size();
		int pages=(a%5==0)?a/5:(a/5+1);
		PageHelper.startPage(page, pageSize);
		List<AlarmRulesHostEntity> indexList = dao.selectMonitorIndex(monitoringIndex);
		List<AlarmRulesHostVO> dtoList = new ArrayList<AlarmRulesHostVO>();

		List<AlarmRulesHostEntity> resultList = dao.selectAll(monitoringIndex);

		for(int i=0;i<indexList.size();i++){
			AlarmRulesHostVO dto = new AlarmRulesHostVO();
			HashMap<String,String> ipMap=new HashMap<String,String>(); 
			for(AlarmRulesHostEntity entity : resultList){
				if(entity.getMonitorIndex().equals(indexList.get(i).getMonitorIndex())){
					dto.setDesc(entity.getMonitorIndex());
					dto.setName(entity.getKpi());
					//保存ip信息 
					ipMap.put(entity.getHostid()+"",entity.getBelongAppliedSystem());
					if(entity.getLevels().equals("1")){
						dto.setLv1min(entity.getMin());
						dto.setLv1max(entity.getMax());
					}else if(entity.getLevels().equals("2"))
					{
						dto.setLv2min(entity.getMin());
						dto.setLv2max(entity.getMax());
					}else if(entity.getLevels().equals("3"))
					{
						dto.setLv3min(entity.getMin());
						dto.setLv3max(entity.getMax());
					}
				}//if结束

			}//内层for
			dto.setId(indexList.get(i).getId());
			dto.setIpMap(ipMap);
			dtoList.add(dto);
		}//外层for
		PageInfo<AlarmRulesHostVO> repageInfo = new PageInfo<AlarmRulesHostVO>(dtoList);
		repageInfo.setTotal(a);
		repageInfo.setPages(pages);
		return repageInfo;
	}

	@Override
	public List<AlarmRulesHostEntity> selectMonitorIndex() {
		
		return dao.selectMonitorIndex(null);
	}

	/*	
	 * 保存配置方法
	 * 
	 * */
	@Override
	public int updateConfig(AlarmRulesHostidDto alarmConfigParam) {
		//第一步删除原有配置
		dao.deleteConfig(alarmConfigParam.getDesc());
		//第二部保存修改后的配置
		return insertConfig(alarmConfigParam);
}
	
	/*	
	 * 新增告警个性化配置方法
	 * 
	 * */
	@Override
	public int insertConfig(AlarmRulesHostidDto alarmConfigParam) {
		int flag =0;
		List<String> emp = new ArrayList<>();

		// 参数调整
		if (alarmConfigParam.getHostids().contains(",")) {
			String[] ids = alarmConfigParam.getHostids().split(",");
			for (int i = 0; i < ids.length; i++) {
				if (ids[i] != null || ids[i] != "") {
					emp.add(ids[i]);
				}
			}
		}
		int n=emp.size();
		AlarmRulesHostidDto param = new AlarmRulesHostidDto();
		for(int index=0;index<n;index++) {
			if(!alarmConfigParam.getWarningLv1ValueLow().equals("null") && !alarmConfigParam.getWarningLv1ValueLow().equals("")){
				param.setLevel("1");
				param.setDesc(alarmConfigParam.getDesc());
				param.setKpi(alarmConfigParam.getKpi());
				param.setMax(alarmConfigParam.getWarningLv1ValueHigh());
				param.setMin(alarmConfigParam.getWarningLv1ValueLow());
				param.setHostid( Integer.parseInt(emp.get(index).toString()));
				flag=dao.insertConfig(param);
			}
			if(!alarmConfigParam.getWarningLv2ValueLow().equals("null") && !alarmConfigParam.getWarningLv2ValueLow().equals("")){
				param.setLevel("2");
				param.setDesc(alarmConfigParam.getDesc());
				param.setKpi(alarmConfigParam.getKpi());
				param.setMax(alarmConfigParam.getWarningLv2ValueHigh());
				param.setMin(alarmConfigParam.getWarningLv2ValueLow());
				param.setHostid( Integer.parseInt(emp.get(index).toString()));
				flag=dao.insertConfig(param);
			}
			if( !alarmConfigParam.getWarningLv3ValueLow().equals("null") && !alarmConfigParam.getWarningLv3ValueLow().equals("") ){
				param.setLevel("3");
				param.setDesc(alarmConfigParam.getDesc());
				param.setKpi(alarmConfigParam.getKpi());
				param.setMax(alarmConfigParam.getWarningLv3ValueHigh());
				param.setMin(alarmConfigParam.getWarningLv3ValueLow());
				param.setHostid( Integer.parseInt(emp.get(index).toString()));
				flag=dao.insertConfig(param);
			}
		}
		return flag;
	}



	/* 
	 *更新选择ip操作
	 */
	@Override
	public int updateWarningPersonalizedSelectIps(AlarmRulesHostidDto alarmRulesHostidDto) {
		// TODO Auto-generated method stub
		
		int flag =0;
		//第一步删除勾选掉的ip
		String delIds=alarmRulesHostidDto.getDeletedHostids();
		if(!delIds.equals("") && !delIds.equals("null")) {  //没有删除id 不要调用删除方法
			List<String> temp =new ArrayList<>();
			if (delIds.contains(",")) {
				String[] ids = delIds.split(",");
				for (int i = 0; i < ids.length; i++) {
					if (ids[i] != null || ids[i] != "") {
						temp.add(ids[i]);
					}
				}
			}
			AlarmRuleHostDeleteDto adelete=new AlarmRuleHostDeleteDto();
			adelete.setDesc(alarmRulesHostidDto.getDesc());
			adelete.setList(temp);
			flag=dao.deleteSelectIps(adelete);
		}
		
		
		//第二部添加新选择的ip
		
	
		List<String> emp = new ArrayList<>();

		// 参数调整
		if (alarmRulesHostidDto.getHostids().contains(",")) {
			String[] ids = alarmRulesHostidDto.getHostids().split(",");
			for (int i = 0; i < ids.length; i++) {
				if (ids[i] != null || ids[i] != "") {
					emp.add(ids[i]);
				}
			}
		}
		int n=emp.size();
		AlarmRulesHostidDto param = new AlarmRulesHostidDto();
		for(int index=0;index<n;index++) {
			if(!alarmRulesHostidDto.getWarningLv1ValueLow().equals("null") && !alarmRulesHostidDto.getWarningLv1ValueLow().equals("")){
				param.setLevel("1");
				param.setDesc(alarmRulesHostidDto.getDesc());
				param.setKpi(alarmRulesHostidDto.getKpi());
				param.setMax(alarmRulesHostidDto.getWarningLv1ValueHigh());
				param.setMin(alarmRulesHostidDto.getWarningLv1ValueLow());
				param.setHostid( Integer.parseInt(emp.get(index).toString()));
				flag=dao.insertConfig(param);
			}
			if(!alarmRulesHostidDto.getWarningLv2ValueLow().equals("null") && !alarmRulesHostidDto.getWarningLv2ValueLow().equals("")){
				param.setLevel("2");
				param.setDesc(alarmRulesHostidDto.getDesc());
				param.setKpi(alarmRulesHostidDto.getKpi());
				param.setMax(alarmRulesHostidDto.getWarningLv2ValueHigh());
				param.setMin(alarmRulesHostidDto.getWarningLv2ValueLow());
				param.setHostid( Integer.parseInt(emp.get(index).toString()));
				flag=dao.insertConfig(param);
			}
			if( !alarmRulesHostidDto.getWarningLv3ValueLow().equals("null") && !alarmRulesHostidDto.getWarningLv3ValueLow().equals("") ){
				param.setLevel("3");
				param.setDesc(alarmRulesHostidDto.getDesc());
				param.setKpi(alarmRulesHostidDto.getKpi());
				param.setMax(alarmRulesHostidDto.getWarningLv3ValueHigh());
				param.setMin(alarmRulesHostidDto.getWarningLv3ValueLow());
				param.setHostid( Integer.parseInt(emp.get(index).toString()));
				flag=dao.insertConfig(param);
			}
		}
		return flag;
	}
	
	/*	
	 * 删除告警个性化配置方法
	 * 
	 * */
	@Override
	public int deleteConfig(String desc) {
		
		return dao.deleteConfig(desc);
	}
	
}
